package com.copyctrl.campus.portal.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.copyctrl.campus.portal.dto.OrderDTO;
import com.copyctrl.campus.portal.service.OrderService;
import com.copyctrl.mapper.CampAddressMapper;
import com.copyctrl.mapper.CampGoodsMapper;
import com.copyctrl.mapper.CampOrderMapper;
import com.copyctrl.model.CampAddress;
import com.copyctrl.model.CampGoods;
import com.copyctrl.model.CampOrder;
import com.copyctrl.model.CampOrderExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author:JH
 * @version:1.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    CampOrderMapper mapper;
    @Autowired
    CampGoodsMapper goodsMapper;
    @Autowired
    CampAddressMapper addressMapper;

    @Override
    public int addOrder(CampOrder order) {
        Integer gid = order.getGid();
        CampGoods campGoods = goodsMapper.selectByPrimaryKey(gid);
        Integer number = campGoods.getNumber();
        if (number > 0) {
            campGoods.setNumber(number-1);
            goodsMapper.updateByPrimaryKeySelective(campGoods);
            return mapper.insert(order);
        }
        return 0;
    }

    @Override
    public int deleteOrder(Integer order) {
        return mapper.deleteByPrimaryKey(order);
    }

    @Override
    public Map<String,List<OrderDTO>> findByUid(Integer uid) {
        CampOrderExample campOrderExample = new CampOrderExample();
        campOrderExample.createCriteria().andUidEqualTo(uid);
        Map<String,List<OrderDTO>> result = new HashMap<>();
        List<OrderDTO> all = mapper.selectByExample(campOrderExample).stream().map(
                campOrder -> {
                    OrderDTO orderDTO = BeanUtil.copyProperties(campOrder, OrderDTO.class);
                    Integer gid = orderDTO.getGid();
                    CampGoods campGoods = goodsMapper.selectByPrimaryKey(gid);
                    orderDTO.setGoods(campGoods);
                    Integer aid = orderDTO.getAid();
                    CampAddress campAddress = addressMapper.selectByPrimaryKey(aid);
                    orderDTO.setAddress(campAddress);
                    return orderDTO;
                }
        ).collect(Collectors.toList());
        result.put("all",all);
        campOrderExample.clear();
        campOrderExample.createCriteria().andUidEqualTo(uid).andFinishTimeIsNull();
        List<OrderDTO> not = mapper.selectByExample(campOrderExample).stream().map(
                campOrder -> {
                    OrderDTO orderDTO = BeanUtil.copyProperties(campOrder, OrderDTO.class);
                    Integer gid = orderDTO.getGid();
                    CampGoods campGoods = goodsMapper.selectByPrimaryKey(gid);
                    orderDTO.setGoods(campGoods);
                    Integer aid = orderDTO.getAid();
                    CampAddress campAddress = addressMapper.selectByPrimaryKey(aid);
                    orderDTO.setAddress(campAddress);
                    return orderDTO;
                }
        ).collect(Collectors.toList());
        result.put("not",not);
        return result;
    }

    @Override
    public int updateOrder(CampOrder order) {
        return mapper.updateByPrimaryKeySelective(order);
    }
}
